<?php

namespace App\Http\Controllers;

use App\Http\Resources\BaseResource;
use App\Http\Resources\ErrorResource;
use App\Imports\PolicyBatchUpload\QnPolicy\RoundBatchUpload;
use App\Repositories\Contracts\QnPolicyInterface;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
use MakeRep\Controllers\BaseController;
use MakeRep\Resources\BaseCollection;
use MakeRep\Services\ParamService;

class QnPolicyController extends BaseController
{
    public function __construct(QnPolicyInterface $interface)
    {
        parent::__construct($interface);
    }

    /**
     * 单程政策页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function single()
    {
        return view('qnpolicy.single');
    }

    /**
     * 往返普通政策页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function round()
    {
        return view('qnpolicy.round');
    }

    /**
     * 批量导入form表单
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function policyuploadform()
    {
        return view('qnpolicy.policyuploadform');
    }

    public function ajaxAdd(Request $request)
    {
        $res = $this->interface->create($request->all());
        if ($res === true) {

            return new BaseResource([]);
        }

        return new ErrorResource(['errmsg'=>$res]);
    }

    /**
     * 列表
     * @param Request $request
     * @return BaseCollection
     */
    public function ajaxLists(Request $request){

        $pageSize = $request->page_size ? : 10;
        $orderBy = $request->order_by ? : 'desc';
        if (in_array(30,$request->admin_platform)) {

            $where = ParamService::createCondition($request,isset($this->interface->fillable) ? $this->interface->fillable : []);
            $where[] = ['platform', '>=', 30];
            $where[] = ['platform', '<', 40];
            $res = $this->interface->pageLists($where,'*',$pageSize,'id',$orderBy);
            return new BaseCollection($res);
        }

        return new BaseCollection(new LengthAwarePaginator([],0,10));
    }

    /**
     * 转到详情页面
     */
    public function update(Request $request)
    {
        $where = $request->id ? ['id' => $request->id, 'admin_id' => request()->admin['id']] : ['admin_id' => request()->admin['id']];

        $res = $this->interface->index($where,'*',['detail']);

        if ($res) {
            $policy = $res;
            $policy['detail']['product_mark'] = explode(',', $policy['detail']['product_mark']);
            $policyInfo = ['policy' => $policy];
            return view('qnpolicy/update', $policyInfo);
        }
        abort(404);
    }

    /**
     * 转到详情页面
     */
    public function singleUpdate(Request $request)
    {
        $where = $request->id ? ['id' => $request->id, 'admin_id' => request()->admin['id']] : ['admin_id' => request()->admin['id']];

        $res = $this->interface->index($where,'*',['detail']);

        if ($res) {
            $policy = $res;
            $policy['detail']['product_mark'] = explode(',', $policy['detail']['product_mark']);
            $policyInfo = ['policy' => $policy];
            return view('qnpolicy/singleUpdate', $policyInfo);
        }
        abort(404);
    }

    /**
     * 更新政策
     */
    public function ajaxUpdate(Request $request)
    {
        $res = $this->interface->updatePolicy($request->all());
        if ($res === true) {

            return new BaseResource([]);
        }
        return new ErrorResource(['errmsg' => $res]);
    }


    /**
     * 删除政策
     * @param Request $request
     * @return BaseResource|ErrorResource
     */
    public function ajaxDelete(Request $request)
    {
        $res = $this->interface->deletePolicy($request->all());
        if ($res) {

            return new BaseResource([]);
        }

        return new ErrorResource(['errmsg'=>$res]);
    }

    /**
     * Round 政策批量上传
     * @param Request $request
     * @return BaseResource
     */
    public function roundUpload(Request $request)
    {

        //先保存第一行程，
        $res = $this->interface->create($request->all());
        if ($res === true) {

            //保存第二行程
            $resSecond = $this->interface->saveSecondPolicy($request->all());
            if ($resSecond === true) {

                return new BaseResource([]);
            }

        }

        return new ErrorResource(['errmsg'=>$res]);
    }

    /**
     * Round 政策批量上传
     * @param Request $request
     * @return BaseResource
     */
    public function roundBatchUpload(Request $request)
    {

       if(Storage::exists( $request->path)){
           $roundBatchUpload = new RoundBatchUpload();
           Excel::import($roundBatchUpload, Storage::path($request->path));
           if ($roundBatchUpload->uploadResult) {
               return new BaseResource([]);
           }
           return new ErrorResource(['errmsg'=>'导入失败，请稍后重试！','errcode'=>4001]);
       } else {
           return new ErrorResource(['errmsg'=>'文件未正确上传！','errcode'=>4001]);
       }

    }
}